package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.mapper.SurveyDrawMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyDrawService;
import org.apache.ibatis.annotations.Results;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.SurveyDrawEntity;

import javax.annotation.Resource;


/**
 * 勘测图纸Service业务层处理
 * @author wutao
 * @date 2024-05-29
 */
@Service
public class SurveyDrawServiceImpl extends ServiceImpl<SurveyDrawMapper, SurveyDrawEntity> implements ISurveyDrawService {

    @Resource
    private SurveyDrawMapper surveyDrawEntityMapper;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询勘测图纸
     * @param id 勘测图纸ID
     * @return 勘测图纸
     */
    @Override
    public SurveyDrawEntity selectSurveyDrawEntityById (String id) {
        return surveyDrawEntityMapper.selectSurveyDrawEntityById ( id );
    }

    /**
     * 查询勘测图纸列表
     * @param surveyDrawEntity 勘测图纸
     * @return 勘测图纸
     */
    @Override
    public List<SurveyDrawEntity> selectSurveyDrawEntityList (SurveyDrawEntity surveyDrawEntity) {
        return surveyDrawEntityMapper.selectSurveyDrawEntityList ( surveyDrawEntity );
    }

    /**
     * 新增勘测图纸
     * @param surveyDrawEntity 勘测图纸
     * @return 结果
     */
    @Override
    public int insertSurveyDrawEntity (SurveyDrawEntity surveyDrawEntity) {
        String drawId = SnowflakeIdWorker.getInstance ().nextStringId ();
        surveyDrawEntity.setId ( drawId );
        surveyDrawEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        surveyDrawEntity.setCreateTime ( DateUtils.getNowDate () );
        uploadFile ( surveyDrawEntity.getFileEntities (), drawId );
        return surveyDrawEntityMapper.insertSurveyDrawEntity ( surveyDrawEntity );
    }

    /**
     * 修改勘测图纸
     * @param surveyDrawEntity 勘测图纸
     * @return 结果
     */
    @Override
    public int updateSurveyDrawEntity (SurveyDrawEntity surveyDrawEntity) {
        surveyDrawEntity.setUpdateTime ( DateUtils.getNowDate () );
        surveyDrawEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        uploadFile ( surveyDrawEntity.getFileEntities (), surveyDrawEntity.getId () );
        return surveyDrawEntityMapper.updateSurveyDrawEntity ( surveyDrawEntity );
    }

    /**
     * 批量删除勘测图纸
     * @param ids 需要删除的勘测图纸ID
     * @return 结果
     */
    @Override
    public int deleteSurveyDrawEntityByIds (String[] ids) {
        return surveyDrawEntityMapper.deleteSurveyDrawEntityByIds ( ids );
    }

    /**
     * 删除勘测图纸信息
     * @param id 勘测图纸ID
     * @return 结果
     */
    @Override
    public int deleteSurveyDrawEntityById (String id) {
        return surveyDrawEntityMapper.deleteSurveyDrawEntityById ( id );
    }

    private void uploadFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.saveBatch ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改勘测图纸附件失败" );
            }
        }
    }

}
